<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" :visible.sync="visible" width="1080px" top="5vh" append-to-body>

    <el-form :model="queryParams" ref="queryForm" size="small" inline>
      <el-form-item label="姓名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input v-model="queryParams.phonenumber" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="small" @click="resetQuery">重置</el-button>
        <el-button size="small" type="primary" @click="handleQuery">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="userTable" v-loading="loading" :data="dataList" fit border @selection-change="handleSelectionChange"
      height="500">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" key="userName" prop="userName" />
      <el-table-column label="登录名" align="center" key="nickName" prop="nickName" />
      <el-table-column label="所属组织机构" align="center" key="deptName" prop="dept.deptName" />
      <el-table-column label="手机号" align="center" key="phonenumber" prop="phonenumber" />
      <el-table-column label="角色" align="center" key="roles" prop="roles">
        <template slot-scope="scope">
          <div v-for="(item, index) in scope.row.roles" :key="index">
            {{ item.roleName }}
          </div>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleSelectUser">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listUser } from '@/api/system/user'
import { cloneDeep } from "lodash"

export default {
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      visible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],

      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      dataList: [],
      // 弹出层标题
      title: '',

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
      },
      multipleSelection: [],
      selected: []
    };
  },
  methods: {
    // 显示弹框
    show(selected = []) {
      this.visible = true;
      this.multipleSelection = selected
      this.selected = selected
      this.resetForm('queryForm')
      this.getList()
    },

    close() {
      this.visible = false
    },

    handleSelectUser() {
      const selected = cloneDeep(this.selected).filter(o => typeof o === 'object')
      // 去重
      const ids = [...new Set(selected.map(item => item.userId))]

      this.$emit("select", ids.map(userId => selected.find(o => o.userId == userId)))
      this.$refs.userTable.clearSelection()
      this.$nextTick(() => {
        this.visible = false
      })
    },

    handleSelectionChange(val) {
      this.multipleSelection = val;
      const list = this.dataList.map(item => {
        item.checked = val.some(o => o.userId == item.userId)
        return item
      })

      this.selected = [...this.selected, ...val].filter(item => {
        const o = list.find(o => o.userId == item.userId)
        return (o && o.checked) || o === undefined
      })
    },

    /** 查询用户列表 */
    getList() {
      this.loading = true
      const selected = cloneDeep(this.selected)
      listUser(this.addDateRange(this.queryParams)).then(response => {
        this.dataList = response.data
        this.total = response.total
        this.loading = false


        this.$nextTick(() => {
          response.data.forEach(row => {
            if (selected.some(data => data == row.userId || data.userId == row.userId)) {
              this.$refs.userTable.toggleRowSelection(row, true);
            } else {
              this.$refs.userTable.toggleRowSelection(row, false);
            }
          })
        })
      }
      )
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
  }
};
</script>
